<!--
 * @Author: Rain
 * @Date: 2021-11-24 22:02:34
 * @LastEditTime: 2024/01/16 11:17:58
 * @Description: Do Something
-->
<template>
  <div class="app-container h-full overflow-hidden flex flex-col">
    <!--工具栏-->
    <div class="head-container h-full flex flex-col">
      <div class="flex items-center flex-end search pb-10">
        <!-- 搜索 -->
        <el-input
          v-model="query.title"
          clearable
          placeholder="标题"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.atmrId"
          filterable
          placeholder="请选择测评类型"
          clearable
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.atmrName"
            :value="item.id"
          />
        </el-select>
        <rrOperation :crud="crud" />
        <crudOperation :permission="permission" />
      </div>
      <div class="w-full flex-1 overflow-hidden">
        <el-table
          ref="table"
          v-loading="crud.loading"
          :data="crud.data"
          size="small"
          style="width: 100%"
          height="100%"
          @selection-change="crud.selectionChangeHandler"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="atmrId" label="测评单元">
            <template slot-scope="{ row }">
              <div>
                {{ row.atmrId | typeFitler(typeList) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="limitNum" label="人数上限" />
          <el-table-column prop="price" label="测评费用" align="center" />
          <el-table-column prop="reportPrice" label="报告费用" align="center" />
          <el-table-column prop="limitUsed" label="已用人数" align="center">
            <template slot-scope="{ row }">
              <span
                class="cursor-pointer text-blue-300"
                v-text="row.limitUsed"
              />
            </template>
          </el-table-column>
          <el-table-column label="团队报告" align="center">
            <template>
              <div
                class="el-icon-s-data text-blue-300 cursor-pointer"
                style="font-size: 24px"
              />
            </template>
          </el-table-column>
          <el-table-column prop="validityTo" label="有效期">
            <template slot-scope="{ row }">
              {{ row.validityFrom }} - {{ row.validityTo }}
            </template>
          </el-table-column>
          <el-table-column prop="qrCode" label="测评二维码">
            <template slot-scope="{ row }">
              <img
                :src="row.qrCode | imgTransform"
                alt=""
                style="width: 60px; height: 60px"
                class="cursor-pointer"
                @click="downUrl(row)"
              >
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              {{ dict.label.advert_status[scope.row.status] }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="checkPer(['admin', 'groupTest:edit', 'groupTest:del'])"
            label="操作"
            width="220px"
            align="center"
          >
            <template slot-scope="scope">
              <div class="flex items-center" style="white-space: nowrap">
                <udOperation :data="scope.row" :permission="permission" />
                <el-button
                  type="primary"
                  size="mini"
                  style="margin: 0 3px"
                  icon="el-icon-s-data"
                  title="报告分析"
                  @click="reportAction(scope.row, 1)"
                />
                <el-button
                  type="primary"
                  size="mini"
                  icon="el-icon-download"
                  title="报告下载"
                  @click="reportAction(scope.row, 2)"
                />
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <pagination />
    </div>
    <!--表单组件-->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="closeWin"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="550px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            class="w-full"
            placeholder="输入标题..."
          />
        </el-form-item>
        <el-form-item label="测评单元" prop="atmrId">
          <el-select
            v-model="form.atmrId"
            filterable
            placeholder="请选择"
            class="w-full"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.atmrName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="测评人数" prop="limitNum">
          <el-input-number
            v-model="form.limitNum"
            placeholder="请输入人数"
            class="w-full"
          />
        </el-form-item>
        <el-form-item label="测评费用" prop="price">
          <el-input v-model="form.price" placeholder="请输入测评费用" />
        </el-form-item>
        <el-form-item label="报告费用" prop="reportPrice">
          <el-input v-model="form.reportPrice" placeholder="请输入报告费用" />
        </el-form-item>
        <el-form-item
          label="有效期"
          prop="dateRange"
          :rules="[
            { required: true, message: '请选择有效期', trigger: 'change' },
          ]"
        >
          <el-date-picker
            v-model="form.dateRange"
            class="w-full"
            placeholder="开始时间"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            type="datetimerange"
            value-format="timestamp"
            @change="selectTime"
          />
        </el-form-item>
        <el-form-item
          label="状态"
          prop="status"
          :rules="[
            { required: true, message: '请选择状态', trigger: 'change' },
          ]"
        >
          <el-select
            v-model="form.status"
            filterable
            placeholder="请选择"
            class="w-full"
          >
            <el-option
              v-for="item in dict.advert_status"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudGroupTest from '@/api/groupTest'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  title: null,
  atmrId: null,
  limitNum: null,
  limitUsed: '',
  status: '0',
  qrCode: '',
  validityFrom: null,
  validityTo: null,
  dateRange: [],
  price: '',
  reportPrice: ''
}
export default {
  name: 'GroupTest',
  components: { pagination, crudOperation, rrOperation, udOperation },
  filters: {
    typeFitler(value, typeList) {
      const item = typeList.find((item) => item.id * 1 === value * 1)
      if (item) {
        return item.atmrName
      }
      return ''
    },
    optionFitler(item, list) {
      const { atmrName, atmrType } = item
      const type = list.find((i) => i.value === atmrType)
      return type && `${atmrName}-${type.label}`
    }
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['advert_status', 'test_type'],
  cruds() {
    return CRUD({
      title: '团体测评',
      url: 'api/groupTest',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudGroupTest }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'groupTest:add'],
        edit: ['admin', 'groupTest:edit'],
        del: ['admin', 'groupTest:del']
      },
      rules: {
        title: [{ required: true, message: '输入测评标题', trigger: 'blur' }],
        atmrId: [
          { required: true, message: '选择测试类型', trigger: 'change' }
        ],
        limitNum: [
          { required: true, message: '输入测试人数', trigger: 'blur' }
        ],
        price: [{ required: true, message: '输入测评费用', trigger: 'blur' }],
        reportPrice: [
          { required: true, message: '输入报告费用', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'title', display_name: '标题' },
        { key: 'atmrId', display_name: '测试类型' }
      ],
      typeList: []
    }
  },
  mounted() {
    this.queryTestType()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit]() {
      const { validityFrom, validityTo, status } = this.form
      Object.assign(this.form, {
        dateRange: [
          new Date(validityFrom).valueOf(),
          new Date(validityTo).valueOf()
        ],
        status: status + ''
      })
    },
    [CRUD.HOOK.beforeToAdd]() {
      this.form.dateRange = []
    },
    closeWin() {
      this.$refs['form'].resetFields()
      this.$refs['form'].clearValidate()
      this.crud.cancelCU()
    },
    // 查询测试类型
    queryTestType() {
      this.$http.get(`/api/atmr`).then((_) => {
        const { content } = _
        this.typeList = content
      })
    },
    selectTime(e) {
      if (e && e.length > 0) {
        this.form.validityFrom = e[0]
        this.form.validityTo = e[1]
      }
    },
    downUrl(row) {
      const { qrCode } = row
      const alink = document.createElement('a')
      alink.href = qrCode
      alink.download = row.title
      alink.click()
    },
    reportAction(row, flag) {}
  }
}
</script>

<style scoped></style>
